我会将BLL与DAL分开作为最佳做法。我通过界面在BLL和DAL之间进行交互。 例如:
public interface IProductRepository
{
void Add(Product myProduct);
Product Get(string name);
Product GetById(int id);
}
业务对象产品是:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
BLL课程是:
public class ProductManager
{
private readonly IProductRepository productRepository;
public ProductManager(IProductRepository productRepository)
{
this.productRepository = productRepository ?? throw new Exception("message");
}
public void AddProduct(Product myProduct)
{
try
{
// Here code validation ecc....
// Add product to database
productRepository.Add(myProduct);
}
catch(Exception e)
{
// Handle exception
}
}
public Product GetProduct(string name)
{
try
{
// Here code to validation ecc....
// Get product from database
var product = _productRepository.Get(name);
return product;
}
catch(Exception e)
{
// Handle exception
}
}
// ecc ecc
}
其中DAL(我将使用实体框架)是:
public ProductRepository : IProductRepository
{
public void Add(Product myProduct)
{
using(var dbContext = MyDbContext())
{
var dbProduct = new PRODUCTS
{
NAME = myProduct.Name,
PRICE = myProduct.Price
}
dbContext.PRODUCT.Add(dbProduct);
dbContext.SaveChanges();
}
}
// ecc ecc
}
现在我有一些问题: - 这是正确的实施吗? - 如果我想插入产品但是我想检查db上是否有相同名称的产品,我是先调用Get方法还是在DAL中插入逻辑,如:
var dbProduct = dbContext.PRODUCTS.FirstOrDefault(p => p.NAME == name);
if(dbProduct == null) .... // insert else throw exception
- 以这种方式使用EntityFramework是对的,还是我失去了linq的所有好处? 对不起,但我很困惑。
谢谢。
答案 0 :(得分:0)
最好使用 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_CANCELED) {
return;
}
try {
if (requestCode == GALLERY && data != null) {
Bitmap bitmap = getBitmapFromData(data, getContext());
File mediaFile = getOutputMediaFile();
String path = saveImage(bitmap, mediaFile);
Log.println(Log.INFO, "Message", path);
Toast.makeText(getContext(), MESSAGE_IMAGE_SAVED, Toast.LENGTH_SHORT).show();
loadImage.setImageBitmap(bitmap);
imageTextMessage.setVisibility(View.INVISIBLE);
} else if (requestCode == CAMERA) {
File file = new File(fileUri.getPath());
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath());
loadImage.setImageBitmap(bitmap);
saveImage(bitmap, file);
Toast.makeText(getContext(), MESSAGE_IMAGE_SAVED, Toast.LENGTH_SHORT).show();
imageTextMessage.setVisibility(View.INVISIBLE);
}
} catch (Exception ex) {
ex.printStackTrace();
Toast.makeText(getContext(), MESSAGE_FAILED, Toast.LENGTH_SHORT).show();
}
}
private Bitmap getBitmapFromData(Intent intent, Context context){
Uri selectedImage = intent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return BitmapFactory.decodeFile(picturePath);
}
private String saveImage(Bitmap bmpImage, File mediaFile) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmpImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
try {
FileOutputStream fo = new FileOutputStream(mediaFile);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(getContext(),
new String[]{mediaFile.getPath()},
new String[]{"image/png"}, null);
fo.close();
return mediaFile.getAbsolutePath();
} catch (IOException ex) {
ex.printStackTrace();
}
return "";
}
来获得更清晰的含义。
.Any()
如果您想要将现有记录恢复为可能编辑,请转到var dbProductExists = dbContext.PRODUCTS.Any(p => p.NAME == name);
if(dbProductExists) .... // insert else throw exception
我认为性能没有差别 - 当他们找到第一次出现时,两者都会停止。