为创建一个模型通用save()方法 [英] Creating a Generic Save() Method for Models
问题描述
我有一个相当简单的系统,并为这一问题的目的,基本上有三个部分:模型,库,应用程序代码
I have a fairly simple system, and for the purposes of this question there are essentially three parts: Models, Repositories, Application Code.
目前的核心是楷模。让我们用一个简单的人为的例子:
At the core are the models. Let's use a simple contrived example:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
在这同一个项目是一个通用库接口。在最简单的:
In that same project is a generic repository interface. At its simplest:
public interface IRepository<T>
{
T Save(T model);
}
实现,在一个单独的项目,并与StructureMap注入。为简单起见:
Implementations of that interface are in a separate project and injected with StructureMap. For simplicity:
public class PersonRepository : IRepository<Person>
{
public Person Save(Person model)
{
throw new NotImplementedException("I got to the save method!");
// In the repository methods I would interact with the database, or
// potentially with some other service for data persistence. For
// now I'm just using LINQ to SQL to a single database, but in the
// future there will be more databases, external services, etc. all
// abstracted behind here.
}
}
所以,在应用程序代码,如果我想保存一个模式,我会做这样的:
So, in application code, if I wanted to save a model I would do this:
var rep = IoCFactory.Current.Container.GetInstance<IRepository<Person>>();
myPerson = rep.Save(myPerson);
简单,但感觉就像它可以自动不少。这种模式拥有整个应用程序的代码,所以我在寻找什么做的就是创建一个通用的保存()
在所有型号上这也只是上面的简写电话应用程序代码。这样,一会只需要调用:
Simple enough, but it feels like it could be automated a lot. That pattern holds throughout the application code, so what I'm looking to do is create a single generic Save()
on all models which would just be a shorthand call to the above application code. That way one would need only call:
myPerson.Save();
但我似乎无法想出一个办法做到这一点。也许这看似简单,我只是没有从正确的角度看它。起初,我试图创建一个空的 ISaveableModel< T>
接口,并打算让每一个拯救-能的模式实现它,那么对于单个通用保存()
方法,我会在界面上的扩展:
But I can't seem to figure out a way to do it. Maybe it's deceptively simple and I'm just not looking at it from the correct angle. At first I tried creating an empty ISaveableModel<T>
interface and intended to have each "save-able" model implement it, then for the single generic Save()
method I would have an extension on the interface:
public static void Save<T>(this ISaveableModel<T> model)
{
var rep = IoCFactory.Current.Container.GetInstance<IRepository<T>>();
model = rep.Save(model);
}
但它告诉我, rep.Save(模型)
具有无效的参数。看来,它不是布线了类型推断为我所希望的那样。我试着用 BaseModel℃的类似的方法; T>
类从该款车型将继承:
But it tells me that rep.Save(model)
has invalid arguments. It seems that it's not wiring up the type inference as I'd hoped it would. I tried a similar approach with a BaseModel<T>
class from which models would inherit:
public class BaseModel<T>
{
public void Save()
{
this = IoCFactory.Current.Container.GetInstance<IRepository<T>>().Save(this);
}
}
但是,编译器误差是相同的。有没有办法达到我想要达到什么目的?我在设计上非常灵活,所以如果我要的东西都错了一个建筑的水平,那么我有房退后一步,改变大局。
But the compiler error is the same. Is there a way to achieve what I'm trying to achieve? I'm very flexible on the design, so if I'm going about something all wrong on an architectural level then I have room to step back and change the big picture.
推荐答案
一下通用的扩展方法解决这个问题。
Would a generic extension method solve it?
public static T Save<T>(this T current)
{
var rep = IoCFactory.Current.Container.GetInstance<IRepository<T>>();
rep.Save(current);
}
您可以将其约束到你的 ISaveableModel< T> ;
接口。返回类型上面没有实现,但你可以把它放到一个布尔值或状态标志,等等。
You can then constrain it to your ISaveableModel<T>
interface. Return type above not implemented, but you can put it to a boolean or status flag, whatever.
这篇关于为创建一个模型通用save()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!